//
//  CJTFirstCityTableViewCell.m
//  BreadTravel
//
//  Created by dllo on 15/12/23.
//  Copyright © 2015年 蓝鸥科技. All rights reserved.
//

#import "CJTFirstCityTableViewCell.h"

@implementation CJTFirstCityTableViewCell

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self aaaa];
    }
    return self;
}

-(void)aaaa
{
    
    self.bigImageView = [[UIImageView alloc] init];
    [self.contentView addSubview:self.bigImageView];
    //设置半径
    self.bigImageView.layer.cornerRadius = 8;
    //裁掉边框
    self.bigImageView.layer.masksToBounds = YES;
    
    self.headImageView = [[UIImageView alloc] init];
    [self.contentView addSubview:self.headImageView];
    //设置边宽
    self.headImageView.layer.borderWidth = 3;
    //设置边颜色
    self.headImageView.layer.borderColor = [UIColor whiteColor].CGColor;
    //设置半径
    self.headImageView.layer.cornerRadius = 30;
    //裁掉边框
    self.headImageView.layer.masksToBounds = YES;

    
    self.titleLabel = [[UILabel alloc] init];
    [self.contentView addSubview:self.titleLabel];

    self.timeLabel = [[UILabel alloc] init];
    [self.contentView addSubview:self.timeLabel];
    
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.bigImageView.mas_left).offset(10);
        make.top.equalTo(self.bigImageView.mas_bottom).offset(15);
        make.width.mas_equalTo(WIDTH*0.6);
        
    }];
    
    
    [self.timeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.titleLabel.mas_left);
        make.right.mas_equalTo(@(-20));
        make.top.equalTo(self.titleLabel.mas_bottom).offset(10);
        
    }];
    
    self.label2 = [[UILabel alloc] init];
    [self.contentView addSubview:self.label2];
    [self.label2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(@-20);
        make.height.mas_equalTo(@25);
        make.width.mas_equalTo(@100);
        make.top.equalTo(self.timeLabel.mas_bottom).offset(10);
    }];
    self.label2.textAlignment = NSTextAlignmentRight;
    
    
    
}

-(void)setModel:(CJTFirstCityModel *)model
{

    NSURL *url = [NSURL URLWithString:model.title_page];
    [self.bigImageView sd_setImageWithURL:url placeholderImage:IMAGE];
    
    [self.bigImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.mas_equalTo(@10);
        make.right.mas_equalTo(@(-10));
        make.height.mas_equalTo(@180);
    
    }];
    
    
    NSURL *url1 = [NSURL URLWithString:model.model.avatar_l];
    [self.headImageView sd_setImageWithURL:url1 placeholderImage:IMAGE];
    [self.headImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.bigImageView.mas_right).offset(-20);
        make.centerY.equalTo(self.bigImageView.mas_bottom);
        make.width.height.mas_equalTo(@60);
            }];
    
    
    self.titleLabel.numberOfLines = 0;
    self.titleLabel.text = model.title;
    NSString *begin_time = @"";
    if (model.date_str.length == 11) {
        NSRange range = NSMakeRange(6, 5);
        begin_time = [model.date_str substringWithRange:range];
    }else{
        begin_time = model.date_str;
    }
    
    
    NSString *str1 = [NSString stringWithFormat:@"%@人喜欢", model.like_count];
    
    NSString *str = [NSString stringWithFormat:@"%@  %@， %@", begin_time, model.address, str1];
    self.timeLabel.text = str;
//    self.timeLabel.backgroundColor = [UIColor orangeColor];
    
    NSString *str2 = [NSString stringWithFormat:@"￥%@", model.price];
    
    NSMutableAttributedString *attribute = [[NSMutableAttributedString alloc]initWithString:str2];
    
    NSRange rangeStr =  [str2 rangeOfString:@"￥"];
    
    [attribute setAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:20],NSForegroundColorAttributeName:[UIColor redColor]} range:rangeStr];
    
    
    self.label2.attributedText = attribute;
    
    [self.labelView removeFromSuperview];
    
    self.labelView = [[UIView alloc] init];
    [self.contentView addSubview:self.labelView];
    
    [self.labelView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.timeLabel.mas_left);
        make.top.equalTo(self.timeLabel.mas_bottom);
        
    }];

    
    for (int i = 0; i < model.tab_list.count; i++) {
        
        self.label1 = [[UILabel alloc] init];
        self.label1.tag = i+1000;
        [self.labelView addSubview:self.label1];
        self.label1.text = model.tab_list[i];
        //设置边宽
        self.label1.layer.borderWidth = 1;
        //设置边颜色
        self.label1.layer.borderColor = [UIColor blackColor].CGColor;
        //设置半径
        self.label1.layer.cornerRadius = 3;
        //裁掉边框
        self.label1.layer.masksToBounds = YES;

        
        
        [self.label1 mas_makeConstraints:^(MASConstraintMaker *make) {
            if (i == 0) {
                make.left.mas_equalTo(@0);
            }else{

            UILabel *tempLabel = [self.contentView viewWithTag:1000+ i - 1];
            make.left.equalTo(tempLabel.mas_right).offset(10);
            }
            make.top.equalTo(self.timeLabel.mas_bottom).offset(10);
            
        }];
        
        
    }
    
    
    
//        CGFloat height = [[self class] heightForContent:model.title];
//    
//        CGRect tempFrame = self.titleLabel.frame;
//        tempFrame.size.height = height;
//        self.titleLabel.frame = tempFrame;
    
}


+(CGFloat)heightForContent:(NSString *)content
{
    //参数1.设置计算高度按照什么样的size来计算
    //宽度一定与label快读一致，高度尽量大一些
    //参数2.设置计算类型
    //参数3.设置计算内容按照多大的字体计算
    //注意：字体大小也要跟label字体一样
    //参数4.系统备用参数，设置为 nil
    //
    CGSize size = CGSizeMake(WIDTH*0.6, 1000000);
    NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:[UIFont systemFontOfSize:17.0], NSFontAttributeName , nil];
    CGRect frame = [content boundingRectWithSize:size options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil];
    return frame.size.height;
}




/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
